package com.wzc.system.service.impl;

import com.wzc.basic.service.impl.BaseServiceImpl;
import com.wzc.system.domain.Role;
import com.wzc.system.mapper.RoleMapper;
import com.wzc.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role){
        // 先保存role，返回自增的主键
        roleMapper.save(role);

        // 再添加关联表
        if(role.getPermissions().size() > 0){
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size() > 0){
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }
    }

    @Override
    public void update(Role role){
        // 修改
        roleMapper.update(role);

        // 先删除所有关联表再添加
        roleMapper.removePermissionByRoleId(role.getId());
        roleMapper.removeMenuByRoleId(role.getId());

        // 再添加关联表
        if(role.getPermissions().size() > 0){
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size() > 0){
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

    }

    @Transactional
    @Override
    public void remove(Long id){
        // 先删除关联表
        roleMapper.removeEmployeeByRoleId(id);
        roleMapper.removePermissionByRoleId(id);
        roleMapper.removeMenuByRoleId(id);
        // 再删role表
        roleMapper.remove(id);

    }



}
